import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import menu from "./modules/menu";
import role from "./modules/role";
import admin from "./modules/admin";
import cate from "./modules/cate";
import specs from "./modules/specs";
import manage from "./modules/manage";
import member from "./modules/member";
import banner from "./modules/banner";
import seckill from "./modules/seckill";

export default new Vuex.Store({
  state: {
    userInfo: sessionStorage.getItem("userInfo") ? JSON.parse(sessionStorage.getItem("userInfo")) : null
  },
  getters: {
    getUser(state) {
      return state.userInfo
    }
  },
  mutations: {
    USERSINFO(state, payload) {
      if (payload) {
        // 有值存储
        state.userInfo = payload
        sessionStorage.setItem("userInfo", JSON.stringify(payload))
      } else {
        // 清空存储
        sessionStorage.removeItem("userInfo")
      }
    }
  },
  actions: {
    changeUserAction({ commit }, data) {
      commit("USERSINFO", data)
    }
  },
  modules: {
    menu,
    role,
    admin,
    cate,
    specs,
    manage,
    member,
    banner,
    seckill
  }
})
